//城市选择
var app = getApp();
Page({
  data: {
    shengArr: [],//省级数组
    shengId: [],//省级id数组
    shiArr: [],//城市数组
    shiId: [],//城市id数组
    quArr: [],//区数组
    shengIndex: 0,
    shiIndex: 0,
    quIndex: 0,
    mid: 0,
    sheng: 0,
    city: 0,
    area: 0,
    code: 0,
    cartId: 0,
    location: [],
    jineStatus: false,
    shijianStatus: false,
    days: [],
    day_index:0,
    warn_index:0,
    userInfo:[],
    havecompany:false,
    nickname:'',
  },
  formSubmit: function (e) {
    var that = this;
    var submit_data = e.detail.value;
    var update = submit_data.update;
    var plen = submit_data.password.length;
    var blen = submit_data.bank.length;
    var bnlen = submit_data.bank_name.length;
    
    var phone = submit_data.phone;
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机号码有误',
        duration: 1000
      });
      return;
    }
    if (bnlen < 2 || plen > 20) {
      wx.showToast({
        title: '银行名称非法',
        duration: 1000
      });
      return;
    }
    if (blen < 16 || plen > 20) {
      wx.showToast({
        title: '卡号非法',
        duration: 1000
      });
      return;
    }
    if (plen < 6 || plen > 20) {
      wx.showToast({
        title: '密码非法',
        duration: 1000
      });
      return;
    }
    if (update == 0) {
      var rplen = submit_data.repassword.length;
      if (submit_data.password != submit_data.repassword) {
        wx.showToast({
          title: '两次密码不一致',
          duration: 1000
        });
        return;
      }
    }
    
    
    wx.request({
      url: app.d.hostUrl + 'wapi/update_userdata.html',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        phone: phone,
        update: update,
        password: submit_data.password,
        bank: submit_data.bank,
        bank_name: submit_data.bank_name,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // success
        var status = res.data.ret;
        if (status == 0) {
          wx.showToast({
            title: '保存成功！',
            duration: 2000
          });
          app.globalData.phone = phone;
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000
          });
        }
        if (that.data.cartId == 'vip1') {
          wx.redirectTo({
            url: '../product/vip1'
          });
          return;
        }
        if (that.data.cartId == 'vip2') {
          wx.redirectTo({
            url: '../product/vip2'
          });
          return;
        }
        wx.navigateBack({
          delta: 1
        });
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })


  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var that = this;

    if (options.cartId != undefined){
      that.setData({
        cartId: options.cartId,
      })
    }

    if (app.globalData.phone && app.globalData.phone != '') {
      that.setData({
        phone: app.globalData.phone,
        havecompany: true
      });
    }
    //if (that.data.phone.length != 11 || that.data.receive == 0) {
    if (that.data.phone == undefined || that.data.receive == 0) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.phoneCallback = phone => {
        if (phone != '') {
          that.setData({
            phone: phone,
            havecompany: true
          });
        }
      }
    }

    if (app.globalData.bank && app.globalData.bank != '') {
      that.setData({
        bank: app.globalData.bank,
      });
    }
    if (app.globalData.receive >= 0) {
      that.setData({
        receive: app.globalData.receive,
      });
    }
    if (app.globalData.bank_name && app.globalData.bank_name != '') {
      that.setData({
        bank_name: app.globalData.bank_name,
      });
    }
    /**console.log(that.data.phone);
    console.log(that.data.receive);
    console.log(that.data.bank);
    console.log(that.data.bank_name);**/

    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      if (userInfo.phone != undefined && userInfo.phone.length == 11){
        //更新数据
        that.setData({
          havecompany:true
        })
      }
      that.setData({
        userInfo: userInfo,
      })
    });
    

  },
  bindPickerChange: function (e) {
    this.setData({
      day_index: e.detail.value
    })
  },
  bindPickerChangeWarn: function (e) {
    this.setData({
      warn_index: e.detail.value
    })
  },
  choose_shijian: function () {
    // 环境中目前已选状态
    var shijianStatus = this.data.shijianStatus;
    // 取反操作
    shijianStatus = !shijianStatus;
    this.setData({
      shijianStatus: shijianStatus,
      jineStatus: false
    });
  },
  choose_jine: function () {
    // 环境中目前已选状态
    var jineStatus = this.data.jineStatus;
    // 取反操作
    jineStatus = !jineStatus;
    this.setData({
      jineStatus: jineStatus,
      shijianStatus: false
    });
  },
  bindPickerChangeshengArr: function (e) {
    this.setData({
      shengIndex: e.detail.value,
      shiArr: [],
      shiId: [],
      quArr: [],
      quiId: []
    });
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/get_province.html',
      data: { province: e.detail.value },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // success
        var status = res.data.status;
        var city = res.data.data;

        var hArr = [];
        var hId = [];
        hArr.push('请选择');
        hId.push('0');
        for (var i = 0; i < city.length; i++) {
          hArr.push(city[i].name);
          hId.push(city[i].id);
        }
        that.setData({
          //sheng:res.data.sheng,
          sheng: e.detail.value,
          shiArr: hArr,
          shiId: hId
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },

    })
  },
  bindPickerChangeshiArr: function (e) {
    this.setData({
      shiIndex: e.detail.value,
      quArr: [],
      quiId: []
    })
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/get_area_name.html',
      data: {
        city: e.detail.value,
        province: this.data.sheng //province
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.status;
        var area = res.data.data;

        var qArr = [];
        var qId = [];
        qArr.push('请选择');
        qId.push('0');
        for (var i = 0; i < area.length; i++) {
          qArr.push(area[i].name)
          qId.push(area[i].id)
        }
        that.setData({
          //city:res.data.city,
          city: e.detail.value,
          quArr: qArr,
          quiId: qId
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
  bindPickerChangequArr: function (e) {
    this.setData({
      quIndex: e.detail.value
    });
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/get_code.html',
      data: {
        quyu: e.detail.value,
        city: this.data.city
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          //area:res.data.area,
          area: e.detail.value,
          //code:res.data.code
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
  onShareAppMessage: function () {
    return {
      title: '赚钱那些事儿',
      path: '/pages/index/index?scene=invite%3D' + app.globalData.user_id,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          duration: 2000
        });
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },
})